import { DocumentHighlight, DocumentHighlightKind } from "vscode-languageserver";
import { t } from "./analysis";
import { getNodeWithPathAtPosition } from "./util";

export async function provideDocumentHighlight(source: t.SourceFile, position: number): Promise<DocumentHighlight[] | undefined> {
    const nodeResult = getNodeWithPathAtPosition(source, position);
    const symbol = nodeResult.node?.symbol;
    if (nodeResult.node && nodeResult.node.isIdentifier() && symbol) {
        const highlights: DocumentHighlight[] = [];
        symbol.tracker.forEachNode(node => {
            let nodeSourceFile = node.getSourceFile();
            if (nodeSourceFile && nodeSourceFile.fileName === source.fileName) {
                highlights.push(DocumentHighlight.create(
                    {
                        start: nodeSourceFile.getLineAndCharactorOfPosition(node.start),
                        end: nodeSourceFile.getLineAndCharactorOfPosition(node.end),
                    },
                    DocumentHighlightKind.Read
                ));
            }
        });
        return highlights;
    }
}
